package binarySearch

func BinarySearch(nums []int, target int) int {
	var low int = 0
	var height int = len(nums) - 1
	//排除极端情况，都是一个数字，并且很长
	if nums[0] == target {
		return 0
	}

	for low <= height {
		var mid int = low + (height-low)/2
		var midValue int = nums[mid]
		if midValue == target {
			for mid != 0 && target == nums[mid-1] {
				mid--
			}
			return mid

		} else if midValue > target {
			height = mid - 1
		} else if midValue < target {

			height = mid + 1

		}
	}

	return -1

}
